import { L2VMessageEnum } from "../Framework/Network/L2VMessageEnum";
import { ObjHandle } from "../Framework/ObjHandle";
import { LogicComponent } from "./LogicComponent";
import { MsgDispatcher } from "../Framework/Network/MsgDispatcher";

export class LogicEntity {

    entityID: number;
    characterID: number;
    self: ObjHandle<LogicEntity> = ObjHandle.NULL;
    components: LogicComponent[] = [];

    init()
    {

    }

    tick(dt: number)
    {

    }

    destroy()
    {
        for(let i = 0; i < this.components.length; i++)
        {
            this.components[i].destroy();
        }

        this.self = null;
    }

    addLogicComponent(component: LogicComponent)
    {
        component.enabled = true;
        this.components.push(component);
    }

    getLogicComponent(componentName: string): LogicComponent
    {
        for(let i = 0; i < this.components.length; i++)
        {
            if(typeof this.components[i] == componentName)
            {
                return this.components[i];
            }
        }
        return null;
    }

    sendEntityMessage(messageID: L2VMessageEnum, ... params: any[])
    {
        let allParams: any[] = [messageID, this.entityID];
        MsgDispatcher.getInstance().dispatchMsg(L2VMessageEnum.EntityMessage, allParams.concat(params));
    }
}
